/**
 * @param {string} s
 * @param {string} chars
 * @param {number[]} vals
 * @return {number}
 */
var maximumCostSubstring = function (s, chars, vals) {
  let res = [],
    left = 0,
    right = 0,
    sub = ''
  while (left < s.length) {
    let index,
      cost = 0

    for (right = left; right <= s.length; right++) {
      sub = s.substring(left, right)
      if (!sub) continue
      let round = 0

      index = chars.indexOf(s[right - 1])
      if (index != -1) {
        round += vals[index]
      } else {
        round += s[right - 1].charCodeAt() - 'a'.charCodeAt() + 1
      }
      if (round < 0) {
        break
      }
      cost += round
    }

    res.push(cost)

    left++
  }
  return Math.max(...res)
}

maximumCostSubstring('abc', 'abc', [-1,-1,-1])
